﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace EasyLottery.Utils
{
    class XmlUtils
    {
        public static object Deserialize(Type type, string xml)
        {
            try
            {
                using (var stringReader = new StringReader(xml))
                {
                    var xmlSerializer = new XmlSerializer(type);
                    return xmlSerializer.Deserialize(stringReader);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        //public static object Deserialize(Type type, Stream stream)
        //{
        //    var xmlSerializer = new XmlSerializer(type);
        //    return xmlSerializer.Deserialize(stream);
        //}

        public static string Serializer(Type type, object obj)
        {
            var memoryStream = new MemoryStream();
            var xmlSerializer = new XmlSerializer(type);
            //序列化对象
            xmlSerializer.Serialize(memoryStream, obj);
            memoryStream.Position = 0;
            var streamReader = new StreamReader(memoryStream);
            var str = streamReader.ReadToEnd();
            streamReader.Dispose();
            memoryStream.Dispose();

            return str;
        }

    }
}
